package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.dao.IUserDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户持久接口实现类
 */
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String pwd) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where u_name=? and u_pwd=?");
            ps.setObject(1,userName);
            ps.setObject(2,pwd);
            rs = ps.executeQuery();

            if(rs.next()){
                return getUser();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }

        return null;
    }

    @Override
    public void add(UserBean user) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_user(u_name,u_pwd,u_birthday,u_money) values(?,?,?,?)");
            ps.setObject(1,user.getName());
            ps.setObject(2,user.getPwd());
            ps.setObject(3,user.getBirthday());
            ps.setObject(4,user.getMoney());

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void del(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from  t_user  where pk_userId=?");
            ps.setObject(1,id);

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void update(Integer id, String newPwd, Integer money) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update  t_user set u_pwd=?,u_money=? where pk_userId=?");
            ps.setObject(1,newPwd);
            ps.setObject(2,money);
            ps.setObject(3,id);

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public CutPageBean<UserBean> cutAll(int pageNO) {
        CutPageBean<UserBean> cutBean = new CutPageBean<>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_user limit ?,?");
            ps.setObject(1,(pageNO - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while(rs.next()){
                cutBean.getList().add(this.getUser());
            }

            ps = con.prepareStatement("select count(*) c from t_user");
            rs = ps.executeQuery();
            if(rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }

        return cutBean;
    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user");
            rs = ps.executeQuery();

            while(rs.next()){
                list.add(getUser());
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }

        return list;
    }

    private UserBean getUser() throws SQLException {
        UserBean user = new UserBean();
        user.setId(rs.getInt("pk_userId"));
        user.setName(rs.getString("u_name"));
        user.setPwd(rs.getString("u_pwd"));
        user.setBirthday(LocalDate.parse(rs.getString("u_birthday")));
        user.setMoney(rs.getInt("u_money"));

        return user;
    }

    @Override
    public UserBean findById(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where pk_userId=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();

            if(rs.next()){
                return getUser();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            this.closeConnection();
        }

        return null;
    }
}
